package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.Employee;
import com.sdry.model.llm.User;
import com.sdry.service.llm.EmployeeService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 员工管理Controller
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:45:15
*/

@Controller
@RequestMapping("/empMana")
public class EmployeeController {

	@Resource
	private EmployeeService empService;  //注入员工service
	
	/**
	 * 分页查询员工信息
	 * @param page 分页条件
	 * @param emp 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageEmp")
	public void listPageEmp(Page page, Employee emp, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (emp.getUserName() != null && !"".equals(emp.getUserName()) ) {
			map.put("userName", emp.getUserName());
		} else {
			map.put("userName", "");
			emp.setUserName("");
		}
		int count = empService.countEmp(emp);
		List<Employee> BomList = empService.listPageEmp(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(BomList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存员工信息
	 * @param emp 员工信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveEmp")
	public void saveEmp(Employee emp, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (emp.getId() == null || "".equals(emp.getId())) {
			isSuccess = empService.insertEmp(emp);
		} else {
			isSuccess = empService.updateEmp(emp);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除员工
	 * @param str uid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteEmp")
	public void deleteEmp(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = empService.deleteEmp(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 查询所有未选的员工
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/selctUserByExemp")
	public void selctUserByExemp(HttpServletResponse response) throws Exception {
		List<User> list = empService.listAllEmp();
		
		//将对象转化为json
        JSONArray json = JSONArray.fromObject(list);
        ResponseUtil.write(response, json);
	}
}
